package com.xyf.service.impl;

import com.xyf.domain.OrderRecords;
import com.xyf.mapper.OrderRecordsMapper;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import com.xyf.mapper.GoodsMapper;
import com.xyf.domain.Goods;
import com.xyf.service.GoodsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private OrderRecordsMapper orderRecordsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Goods record) {
        return goodsMapper.insert(record);
    }

    @Override
    public int insertSelective(Goods record) {
        return goodsMapper.insertSelective(record);
    }

    @Override
    public Goods selectByPrimaryKey(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Goods record) {
        return goodsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Goods record) {
        return goodsMapper.updateByPrimaryKey(record);
    }

    /**
     * @param goodsId
     * @param userId
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void realSeckill(Integer goodsId, Integer userId) {
        // 扣减库存  插入订单表
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        int finalStock = goods.getStocks() - 1;
        if (finalStock < 0) {
            // 只是记录日志 让代码停下来   这里的异常用户无法感知
            throw new RuntimeException("库存不足：" + goodsId);
        }
        goods.setStocks(finalStock);
        goods.setUpdateTime(new Date());
        // insert 要么成功 要么报错  update 会出现i<=0的情况
        // update goods set stocks =  1 where id = 1  没有行锁
        int i = goodsMapper.updateByPrimaryKey(goods);
        if (i > 0) {
            // 写订单表
            OrderRecords orderRecords = new OrderRecords();
            orderRecords.setGoodsId(goodsId);
            orderRecords.setUserId(userId);
            orderRecords.setCreateTime(new Date());
            // 时间戳生成订单号
            orderRecords.setOrderSn(String.valueOf(System.currentTimeMillis()));
            orderRecordsMapper.insert(orderRecords);
        }
    }
}

/**
 * mysql行锁  innodb  行锁
 * 分布式锁
 * todo 答案1
 *
 * @param goodsId
 * @param userId
 */
//    @Override
//    @Transactional(rollbackFor = RuntimeException.class)
//    public void realDoSeckill(Integer goodsId, Integer userId) {
//        // update goods set stocks = stocks - 1 ,update_time = now() where id = #{value}
//        int i = goodsMapper.updateStocks(goodsId);
//        if (i > 0) {
//            // 写订单表
//            OrderRecords orderRecords = new OrderRecords();
//            orderRecords.setGoodsId(goodsId);
//            orderRecords.setUserId(userId);
//            orderRecords.setCreateTime(new Date());
//            // 时间戳生成订单号
//            orderRecords.setOrderSn(String.valueOf(System.currentTimeMillis()));
//            orderRecordsMapper.insert(orderRecords);
//        }
//    }

